package com.ctfo.ulcp.exp.db

import org.jetbrains.exposed.sql.SchemaUtils

data class ProjectResource(val project: Project, val resources: List<Resource>)

fun migrate() {

    DSL.transaction {
        exec("drop table if exists t_project")
        exec("drop table if exists t_resource")
        exec("drop table if exists t_resource_task")
        exec("drop table if exists t_table_descriptor")
        SchemaUtils.create(Projects, Resources, Tasks, TableDescriptors)

        val projectResources = listOf(
            ProjectResource(
                Project(
                    null,
                    "统一权限系统",
                    "ctfo-platform",
                    "统一权限系统是公司内部用于校验用户权限的系统",
                    "千方内部的统一权限系统"
                ),
                listOf(
                    Resource(
                        null,
                        null,
                        ResourceType.DOC,
                        "统一权限认证平台API文档",
                        "这个是统一权限系统",
                        DocProperty("F:\\doc\\王华-工作交接\\1.统一认证和权限系统\\统一权限认证平台API文档v202210.docx", FileExt.DOC, 100)
                    ),
                    Resource(
                        null,
                        null,
                        ResourceType.DB,
                        "统一权限数据库",
                        "统一权限系统使用的MySQL数据库",
                        DbProperty(
                            "jdbc:mysql://172.20.60.19:3306/ctfo_platform_system?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai",
                            "root",
                            "123456",
                            DataSourceType.MySQL
                        )
                    ),
                )
            ),

            ProjectResource(
                Project(
                    null,
                    "重货大模型",
                    "hyptpsjx",
                    "是一个负责管理车辆信息、车辆行驶、报警的系统",
                    "这个是重货大模型"
                ),
                listOf()
            ),
        )



        projectResources.forEach { pr ->
            val project = Projects.create(pr.project)
            val resourceManager = ResourceManagerFactory.resourceManagerForProject(project.id!!)
            resourceManager.dropCollection()
            pr.resources.forEach { res ->
                res.projectId = project.id
                Resources.create(res)
                resourceManager.execSaveResourceInfo(res)
            }
        }

    }
}

fun main() {
    println("start db migrate...")
    arrayOf("")
    migrate()
}

